package com.canxi.earn.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.canxi.common.annotation.RepeatSubmit;
import com.canxi.common.annotation.Log;
import com.canxi.common.core.controller.BaseController;
import com.canxi.common.core.domain.PageQuery;
import com.canxi.common.core.domain.R;
import com.canxi.common.core.validate.AddGroup;
import com.canxi.common.core.validate.EditGroup;
import com.canxi.common.enums.BusinessType;
import com.canxi.common.utils.poi.ExcelUtil;
import com.canxi.earn.domain.vo.CxJokeVo;
import com.canxi.earn.domain.bo.CxJokeBo;
import com.canxi.earn.service.ICxJokeService;
import com.canxi.common.core.page.TableDataInfo;

/**
 * 笑话
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/earn/joke")
public class CxJokeController extends BaseController {

    private final ICxJokeService iCxJokeService;


/**
     * 查询笑话列表
     */
    @SaCheckPermission("earn:joke:list")
    @GetMapping("/list")
    public TableDataInfo<CxJokeVo> list(CxJokeBo bo) {
        return iCxJokeService.queryCxJokeList(bo);
    }


    /**
     * 导出笑话列表
     */
    @SaCheckPermission("earn:joke:export")
    @Log(title = "笑话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CxJokeBo bo, HttpServletResponse response) {
        List<CxJokeVo> list = iCxJokeService.queryList(bo);
        ExcelUtil.exportExcel(list, "笑话", CxJokeVo.class, response);
    }

    /**
     * 获取笑话详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("earn:joke:query")
    @GetMapping("/{id}")
    public R<CxJokeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iCxJokeService.queryById(id));
    }

    /**
     * 新增笑话
     */
    @SaCheckPermission("earn:joke:add")
    @Log(title = "笑话", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CxJokeBo bo) {
        return toAjax(iCxJokeService.insertByBo(bo));
    }

    /**
     * 修改笑话
     */
    @SaCheckPermission("earn:joke:edit")
    @Log(title = "笑话", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CxJokeBo bo) {
        return toAjax(iCxJokeService.updateByBo(bo));
    }

    /**
     * 删除笑话
     *
     * @param ids 主键串
     */
    @SaCheckPermission("earn:joke:remove")
    @Log(title = "笑话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iCxJokeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取数据从其它平台
     * @param bo
     * @return
     */
    @SaCheckPermission("earn:joke:add")
    @Log(title = "笑话", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("getInfoFromJuhe")
    public R<Void> addFromOther(CxJokeBo bo) {
        return toAjax(iCxJokeService.addFromOther(bo));
    }

}
